{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy.io import wavfile\n",
    "import sys\n",
    "sys.path.append(\"../\") \n",
    "import pysepm\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Load Audio Files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:1: WavFileWarning: Chunk (non-data) not understood, skipping it.\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n",
      "/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:2: WavFileWarning: Chunk (non-data) not understood, skipping it.\n",
      "  \n",
      "/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:3: WavFileWarning: Chunk (non-data) not understood, skipping it.\n",
      "  This is separate from the ipykernel package so we can avoid doing imports until\n"
     ]
    }
   ],
   "source": [
    "fs, clean_speech = wavfile.read('1_speech_16000_Hz.wav')\n",
    "fs, noisy_speech = wavfile.read('1_noisySpeech_16000_Hz.wav')\n",
    "fs, enhanced_speech = wavfile.read('1_processed_16000_Hz.wav')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Calc Measures"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## fwSNRseg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.677122381286333"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pysepm.fwSNRseg(clean_speech, noisy_speech, fs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.7018915637639442"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pysepm.fwSNRseg(clean_speech, enhanced_speech, fs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SNRseg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.21686174881958412"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pysepm.SNRseg(clean_speech, noisy_speech, fs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.2033705198493232"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pysepm.SNRseg(clean_speech, enhanced_speech, fs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## LLR"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.2530765533868626"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pysepm.llr(clean_speech, noisy_speech, fs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.6044180854910925"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pysepm.llr(clean_speech, enhanced_speech, fs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## WSS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "44.69861369131994"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pysepm.wss(clean_speech, noisy_speech, fs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "66.79548150633117"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pysepm.wss(clean_speech, enhanced_speech, fs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Cepstrum Distance (CD)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.187803223674154"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pysepm.cepstrum_distance(clean_speech, noisy_speech, fs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8.308018500622689"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pysepm.cepstrum_distance(clean_speech, enhanced_speech, fs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## STOI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8389211808320151"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pysepm.stoi(clean_speech, noisy_speech, fs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.661152205056174"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pysepm.stoi(clean_speech, enhanced_speech, fs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## CSII"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.7336988398405825, 0.4363089356143529, 0.018848854017316796)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pysepm.csii(clean_speech, noisy_speech, fs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.527623436040412, 0.2437425452887004, 0.010118033728508934)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pysepm.csii(clean_speech, enhanced_speech, fs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## PESQ"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(nan, 1.1624178886413574)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pysepm.pesq(clean_speech, noisy_speech, fs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(nan, 1.0594704151153564)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pysepm.pesq(clean_speech, enhanced_speech, fs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Composite"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2.037992006581127, 1.8630831647556954, 1.5433156477547219)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pysepm.composite(clean_speech, noisy_speech, fs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1.0, 1.5970461451303148, 1.0)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pysepm.composite(clean_speech, enhanced_speech, fs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## NCM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "pysepm.ncm(clean_speech, noisy_speech, fs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "pysepm.ncm(clean_speech, enhanced_speech, fs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# SRMR"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python3.6/dist-packages/srmrpy/hilbert.py:69: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.\n",
      "  h = h[ind]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "3.2050299299144114"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pysepm.srmr(noisy_speech, fs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.317370449764798"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pysepm.srmr(enhanced_speech, fs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# BSD"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "include pre-emphasis\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "38086949616.83964"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pysepm.bsd(clean_speech, noisy_speech, fs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "include pre-emphasis\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "51047508.139671564"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pysepm.bsd(clean_speech, enhanced_speech, fs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Measure Execution Times"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "61.1 ms ± 667 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit pysepm.fwSNRseg(clean_speech, noisy_speech, fs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "14.5 ms ± 11.5 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit pysepm.SNRseg(clean_speech, noisy_speech, fs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "113 ms ± 188 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit pysepm.llr(clean_speech, noisy_speech, fs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "80 ms ± 552 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit pysepm.wss(clean_speech, noisy_speech, fs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "92.5 ms ± 391 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit pysepm.cepstrum_distance(clean_speech, noisy_speech, fs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "143 ms ± 381 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit pysepm.stoi(clean_speech, noisy_speech, fs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "82.7 ms ± 450 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit pysepm.csii(clean_speech, noisy_speech, fs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "309 ms ± 510 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
     ]
    }
   ],
   "source": [
    "%timeit pysepm.pesq(clean_speech, noisy_speech, fs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "521 ms ± 1.27 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
     ]
    }
   ],
   "source": [
    "%timeit pysepm.composite(clean_speech, noisy_speech, fs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5.13 s ± 13.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
     ]
    }
   ],
   "source": [
    "%timeit pysepm.ncm(clean_speech, noisy_speech, fs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python3.6/dist-packages/srmrpy/hilbert.py:69: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.\n",
      "  h = h[ind]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.6 s ± 660 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
     ]
    }
   ],
   "source": [
    "%timeit pysepm.srmr(clean_speech, fs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "include pre-emphasis\n",
      "bark filter not tested\n",
      "37.9 ms ± 319 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit pysepm.bsd(clean_speech, noisy_speech, fs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
